package com.isoftstone.studentregistration.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import javax.servlet.http.HttpServletResponse;
import cn.hutool.http.server.HttpServerResponse;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.isoftstone.studentregistration.pojo.Users;
import com.isoftstone.studentregistration.service.UserService;
import com.isoftstone.studentregistration.util.Result;
import org.apache.poi.EmptyFileException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @version v1.0
 * @date 2025/9/7 10:47
 */
@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    private UserService userService;
    private LineCaptcha lineCaptcha;
    @PostMapping("/user")
    public Result<?> insertUser(@RequestBody Users user){
        return userService.insertUser(user);
    }
    @PutMapping("/user")
    public Result<?> updateUser(@RequestBody Users user){
        return userService.updateUser(user);
    }
    @DeleteMapping("/user/{id}")
    public Result<?> deleteUser(@PathVariable Integer id){
        return userService.deleteUser(id);
    }
    @GetMapping("/user")
    public Result<?> selectUser(@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "10") Integer pageSize){
        Users queryCondition = new Users();
//        queryCondition.setStatus(1);
        Page page = userService.queryAllStudents(currentPage,pageSize,queryCondition);

        PageInfo<Object> pageInfo = new PageInfo<>(page);
        return Result.success(pageInfo);
    }
    @GetMapping("/captcha")
    public void captcha(HttpServletResponse response){
         lineCaptcha = CaptchaUtil.createLineCaptcha(125,45,4,100);
         lineCaptcha.setBackground(new Color(181,238,218));
        try {
            ServletOutputStream os = response.getOutputStream();
            lineCaptcha.write(os);
            os.flush();
            os.close();
        }catch (IOException e){
        throw new RuntimeException(e);}
    }
    @PostMapping("/login")
    public Result<?> login(@RequestBody Map<String,String> user){
//        获取验证码
        String code= user.get("code");
        String phone= user.get("phone");
        String password= user.get("password");
        System.out.println(code);
        System.out.println(phone);
        System.out.println(password);
//        验证码验证
        if (!lineCaptcha.verify(code)){
            return Result.error("验证码错误");
        }
        return userService.login(user.get("phone"),user.get("password"));
    }
//导出
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response){
        userService.exportExcel(response);
    }
//    文件导入
    @PostMapping("/import")
    public Result<?> importExcel(MultipartFile file) {
        //获取流对象
        InputStream is = null;
        try {
            is = file.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //读取流对象
        ExcelReader reader= ExcelUtil.getReader(is);
        //读取数据
        List<Users> list = reader.readAll(Users.class);
        //批量插入
        try{
            list.stream().forEach(user->userService.insertUser(user));
        }catch (Exception e){
            return Result.error("导入失败");
        }
        return Result.success("导入成功");
    }
}
